﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BanmaytinhBOL;
using BanmaytinhDAL;
using Banmaytinh.Common;
namespace Banmaytinh.AdminSP
{
    public partial class Hang : System.Web.UI.Page
    {

        private static List<HangInfo> source;
        private const string INSERT = "Thêm";
        private const string UPDATE = "Cập nhật";
        private const string CMD_EDIT = "cmdEdit";
        private const string CMD_DELETE = "cmdDelete";
        private static string Mahang = String.Empty;
        private static HangInfo obj;
        private static int IndexOfObject = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadDataToList();
                BindGrid();
                
                LoadDonvitinh();
            
            }
        }


        private void BindGrid() 
        {
            
            grHang.DataSource = HangBOL.Hang_GetByAll();
            grHang.DataBind();
            
        }
        protected void PanelOnOff()
        {
            Panelview.Visible = !Panelview.Visible;
            Paneledit.Visible = !Paneledit.Visible;

        }
        protected void LoadDataToList() 
        {
            source= new List<HangInfo>();
            source = BanmaytinhBOL.HangBOL.Hang_GetByAll();
        }


        protected void SetValueFromForm()
        {
            if (obj == null)
                obj = new HangInfo();
            obj.Tenhang = txtTenhang.Text;
            obj.Maloai = cboLoaihang.SelectedValue;
            obj.Mahangsanxuat = cboHangsanxuat.SelectedValue;
            obj.Makho = cboKhohang.SelectedValue;
            obj.Madonvitinh = cboDonvitinh.SelectedValue;
            obj.Thoigianbaohanh = txtThoigianbaohanh.Text;
            obj.Thutuhienthi = txtThutuhienthi.Text;
            obj.Tieude = txtThutuhienthi.Text;
            obj.Motasoluoc = txtMotasoluoc.Text;
            obj.Tukhoatimkiem = txtTukhoatimkiem.Text;
            obj.Serial = txtSerial.Text;
            obj.Anh = txtAnh.Text;
            obj.Hienthi = Convert.ToString(chkHienthi.Checked);
        }
        protected void FillValueToForm(HangInfo obj)
        {
            txtTenhang.Text = obj.Tenhang;
            cboLoaihang.SelectedValue = obj.Maloai;
            cboHangsanxuat.SelectedValue = obj.Mahangsanxuat;
            cboKhohang.SelectedValue = obj.Makho;
            cboDonvitinh.SelectedValue = obj.Madonvitinh;
            txtThoigianbaohanh.Text = obj.Thoigianbaohanh;
            txtThutuhienthi.Text = obj.Thutuhienthi;
            txtTieude.Text = obj.Tieude;
            txtMotasoluoc.Content = obj.Motasoluoc;
            txtTukhoatimkiem.Text = obj.Tukhoatimkiem;
            txtSerial.Text = obj.Serial;
            txtAnh.Text = obj.Anh;
            chkHienthi.Checked = Boolean.Parse(obj.Hienthi);
           
            if (!String.IsNullOrEmpty(obj.Anh))
            {
                imgAnh.ImageUrl = obj.Anh;
            }
        }
        protected void grHang_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void LoadDonvitinh()
        {
            List<DonvitinhInfo> list = DonvitinhBOL.Donvitinh_GetByAll();
            cboDonvitinh.DataSource = list;
            cboDonvitinh.DataValueField = "Madonvitinh";
            cboDonvitinh.DataTextField = "Tendonvi";
            cboDonvitinh.DataBind();
        }

        protected void lbtnXoa_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < grHang.Rows.Count; i++)
            {
                GridViewRow row = grHang.Rows[i];
                if (row.RowType == DataControlRowType.DataRow)
                {
                    CheckBox chk = (CheckBox)row.FindControl("chkSelect");
                    if (chk.Checked)
                    {
                        String ID = row.Cells[1].Text;
                        HangBOL.Hang_Delete(ID);

                    }


                }

            }
            grHang.PageIndex = 0;
            BindGrid();
        }

        protected void lbtnThem_Click(object sender, EventArgs e)
        {
            PanelOnOff();
            btnLuu.Text = INSERT;
        }

        protected void btnLuu_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                SetValueFromForm();
                switch (btnLuu.Text)
                {
                    case UPDATE:
                        HangBOL.Hang_Update(obj);
                        BindGrid();
                        break;
                    case INSERT:
                        HangBOL.Hang_Insert(obj);
                        BindGrid();
                        break;
                }
                ControlHelper.ResetControl(Paneledit);
                PanelOnOff();
                obj = null;
            }
        }
        protected void btnHuy_Click(object sender, EventArgs e)
        {
            PanelOnOff();
            ControlHelper.ResetControl(Paneledit);
        }

        protected void grHang_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grHang.PageIndex = e.NewPageIndex;
            BindGrid();
        }

        protected void grHang_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            Mahang = e.CommandArgument.ToString();
            obj = new HangInfo();
            obj = HangBOL.Hang_GetById(Mahang);

            switch (e.CommandName)
            {
                case CMD_DELETE:
                    HangBOL.Hang_Delete(Mahang);
                    BindGrid();
                    break;
                case CMD_EDIT:
                    FillValueToForm(obj);
                    IndexOfObject = source.FindIndex(delegate(HangInfo tmp)
                    {
                        return tmp.Mahang == Mahang;
                    }
                    );
                    btnLuu.Text = UPDATE;
                    PanelOnOff();
                    break;
            }
        }

        protected void lbtnLamlai_Click(object sender, EventArgs e)
        {

        }

       

        
    }
}